package com.ht.medicaltreatment.viewmodel

import com.hjq.http.EasyHttp
import com.hjq.http.listener.OnHttpListener
import com.ht.medicaltreatment.bean.DrawerBean
import com.ht.medicaltreatment.bean.FloorBean
import com.ht.medicaltreatment.bean.ItemListBean
import com.ht.medicaltreatment.bean.RefrigeratorBean
import com.ht.medicaltreatment.http.api.DrawerAPI
import com.ht.medicaltreatment.http.api.FloorAPI2
import com.ht.medicaltreatment.http.api.LocationAPI

class ItemFragmentViewModel : BaseViewModel(){
    //冰箱数据
    fun fridge(onResult:(MutableList<ItemListBean>)->Unit){
       // showProgress()
        EasyHttp.post(this)
            .api("/other/app_refrigeratorStru/queryAllRefrigeratorStru.do")
            .request(object : OnHttpListener<RefrigeratorBean>{
                override fun onHttpSuccess(result: RefrigeratorBean?) {
                    //ProgressUtils.dismiss()
                    val list = mutableListOf<ItemListBean>()
                    result?.data?.forEach {
                        list += ItemListBean(it.id,it.name,"")
                    }
                    onResult(list)
                }

                override fun onHttpFail(e: Exception) {
                    dismissProgress(e.message?:"发生了未知的错误")
                }
            })
    }

    //层数
    fun floor(id:Int,onResult:(MutableList<ItemListBean>)->Unit){
        //showProgress()
        EasyHttp.post(this)
            .api(FloorAPI2(id))
            .request(object : OnHttpListener<FloorBean>{
                override fun onHttpSuccess(result: FloorBean?) {
                    //ProgressUtils.dismiss()
                    val list = mutableListOf<ItemListBean>()
                    result?.data?.forEach {
                        list += ItemListBean(it.id,"${it.location}",it.disabled)
                    }
                    onResult(list)
                }

                override fun onHttpFail(e: Exception) {
                    dismissProgress(e.message?:"发生了未知的错误")
                }
            })
    }

    //抽屉
    fun drawer(id:Int,onResult:(MutableList<ItemListBean>)->Unit){
        //showProgress()
        EasyHttp.post(this)
            .api(DrawerAPI(id))
            .request(object : OnHttpListener<DrawerBean>{
                override fun onHttpSuccess(result: DrawerBean?) {
                    //ProgressUtils.dismiss()
                    val list = mutableListOf<ItemListBean>()
                    result?.data?.forEach {
                        list += ItemListBean(it.id,"${it.location}",it.disabled)
                    }
                    onResult(list)
                }

                override fun onHttpFail(e: Exception) {
                    dismissProgress(e.message?:"发生了未知的错误")
                }
            })
    }

    //位置
    fun location(id:Int,onResult:(MutableList<ItemListBean>)->Unit){
        //showProgress()
        EasyHttp.post(this)
            .api(LocationAPI(id))
            .request(object : OnHttpListener<DrawerBean>{
                override fun onHttpSuccess(result: DrawerBean?) {
                    //ProgressUtils.dismiss()
                    val list = mutableListOf<ItemListBean>()
                    result?.data?.forEach {
                        list += ItemListBean(it.id,"${it.location}",it.disabled?:"")
                    }
                    onResult(list)
                }

                override fun onHttpFail(e: Exception) {
                    dismissProgress(e.message?:"发生了未知的错误")
                }
            })
    }
}